<?php

class Score extends Eloquent {

    public function scopeChuyenCan($query, $Date, $User) {
        $query->where('type', 'CHUYEN_CAN');

        if (is_array($Date)) {
            if (isset($Date['start']) && isset($Date['end'])) {
                $query->where('date', '>=', $Date['start']);
                $query->where('date', '<=', $Date['end']);
            } else {
                $query->whereIn('date', $Date);
            }
        } else {
            $query->where('date', $Date);
        }

        if (is_array($User)) {
            $query->whereIn('user_id', $User);
        } else if ($User) {
            $query->where('user_id', $User);
        }

        return $query;
    }

    public function scopeHocLuc($query, $User, $DOT_KIEM_TRA = null, $LAN_KIEM_TRA = null, $iCourse = null) {
        $query->where('type', 'HOC_LUC');

        $Option = Cache::get('options');

        if (!$iCourse)
            $iCourse = CNamHoa::CurrentCourse();

        $query->where('date', '>=', $Option['COURSE'][$iCourse]->data->start);
        $query->where('date', '<=', $Option['COURSE'][$iCourse]->data->end);

        if (is_array($User)) {
            $query->whereIn('user_id', $User);
        } else {
            $query->where('user_id', $User);
        }

        if ($DOT_KIEM_TRA) {
            $query->where('scoretype', $DOT_KIEM_TRA);
        }

        if ($LAN_KIEM_TRA) {
            $query->where('data', $LAN_KIEM_TRA);
        }

        return $query;
    }

    public function user() {
        return $this->belongsTo('User', 'user_id');
    }

}